package com.bw.kotlindemo.java.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseActivity<VM extends ViewModel, DBD extends ViewDataBinding> extends AppCompatActivity {
    public VM mViewModel;
    public DBD mDataBinding;
    public AppCompatActivity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mActivity = this;
        mDataBinding = DataBindingUtil.setContentView(this, initLayout());
        initViewModel();
        initCoding();
    }

    protected abstract int initLayout();

    protected abstract void initCoding();

    protected void initViewModel() {
        final Type type = getClass().getGenericSuperclass();
        Class cla = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
        mViewModel = (VM) new ViewModelProvider(this).get(cla);
    }

}